package com.itheima.putige.putige.filter;

import com.alibaba.fastjson.JSON;

import com.itheima.putige.putige.common.BaseContext;
import com.itheima.putige.putige.common.R;
import com.itheima.putige.putige.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: LoginCheckFilter
 * Description:
 * date: 2022/2/3 9:31
 * 过滤器，用来过滤掉没有登录的请求
 *
 * @author zjf95
 * @since JDK 1.8
 */

@Slf4j
//@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {

    //进行路径比较的
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    // 1. 判断请求是否符合要求
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //强转
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String requestURI = req.getRequestURI();
        //定义可以直接放行的uri


        log.info("拦截到请求：{}", requestURI);

        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login"
        };
/*
        for (String url : urls) {
            if(url.contains(uri)){

            }
        }*/

        boolean check = check(urls, requestURI);

        //如果这些uri是包含在url里面的就放行
        if (check) {
            log.info("本次请求{}不需要处理", requestURI);

            log.info(req.toString());
            log.info(resp.toString());

            filterChain.doFilter(req, resp);
            return;
        }

       /* Object employee = req.getSession().getAttribute("employee");
        System.out.println(employee);*/

        //网页登录
        if (req.getSession().getAttribute("employee") != null) {
            log.info("网页端用户已登录，用户id为：{}", req.getSession().getAttribute("employee"));

            Employee emp = (Employee) req.getSession().getAttribute("employee");
            BaseContext.setCurrentId(emp.getId());

            Long id = Thread.currentThread().getId();
            log.info("线程id为：{}", id);
            filterChain.doFilter(req, resp);
            return;
        }
        //移动端登陆
        if (req.getSession().getAttribute("user") != null) {
            log.info("移动端用户已登录，用户id为：{}", req.getSession().getAttribute("user"));

            Long userId = (Long) req.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);

            filterChain.doFilter(req, resp);
            return;

        }

        log.info("用户未登录");
        resp.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }

    public boolean check(String[] urls, String uri) {

        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, uri);
            if (match) {
                return true;
            }
        }
        return false;
    }
}
